package cn.pconline.autoclub.service;

import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import cn.pconline.autoclub.entity.Theme;
import cn.pconline.autoclub.util.Pager;

/**
 * @author zhouruxuan
 * @date 2014年10月16日 上午10:58:11
 */
public class ThemeService {

	@Autowired
    GeliDao geliDao;
    @Autowired
    GeliOrm geliOrm;
    @Autowired
    private PagerService pagerService;
    
    /**
     * 分页罗列所有主题
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Pager<Theme> listAllThemePager(int pageNo, int pageSize) {
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql("select theme_id from ").appendSql(geliOrm.getTableName(Theme.class));
    	return pagerService.pager(Theme.class, sql, pageNo, pageSize);
    }


    
    /**
     * 根据themeId查找Theme
     * @param themeId
     * @return
     */
    public Theme findThemeById(long themeId) {
    	try {
    		return geliDao.find(Theme.class, themeId);
		} catch (DataAccessException e) {
			return null;
		}
    	
    }    
    /**
     * 罗列所有主题 
     * 
     * @return list
     */
    public List<Theme> listAllThemes(){
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql("select theme_id from ").appendSql(geliOrm.getTableName(Theme.class));
    	try {
    		List<Theme> list = geliDao.list(Theme.class, sql.getSql(), sql.getValues());
    		return list;
		} catch (DataAccessException e) {
			return null;
		}
    }
    
    /**
     * 根据版块ID，获取所有主题列表
     * @param forumId
     * @return
     */
    public List<Theme> listThemeByForumId(long forumId) {
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql("select theme_id from ").appendSql(geliOrm.getTableName(Theme.class));
    	sql.appendSql(" where 1 = 1");
    	sql.appendSql(" and forum_id = ").appendValue(forumId);
    	try {
    		List<Theme> list = geliDao.list(Theme.class, sql.getSql(), sql.getValues());
    		return list;
		} catch (DataAccessException e) {
			return null;
		}
    }
}
